﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Drill.Core;
using NUnit.Framework;

namespace Drill.Integration.Web
{
	[TestFixture]
	public class AspNetRootPathProviderTests
	{
		private class MockHttpContext : HttpContextBase
		{
			public HttpServerUtilityBase ServerUtility { get; set; }

			public override HttpServerUtilityBase Server { get { return ServerUtility; } }
		}

		private class MockHttpServerUtility : HttpServerUtilityBase
		{
			public string BasePath { get; set; }

			public override string MapPath(string path)
			{
				const string aspNetAppRootPath = "~/";
				const string backSlash = @"\";
				const string forwardSlash = "/";

				string basePath = BasePath;
				if (false == basePath.EndsWith(backSlash))
				{
					basePath += backSlash;
				}

				string mappedPath =
					path.Replace(aspNetAppRootPath, basePath)
						.Replace(forwardSlash, backSlash);

				if (mappedPath.StartsWith(backSlash))
				{
					mappedPath = Path.Combine(basePath, mappedPath);
				}
				return mappedPath;
			}
		}

		[Test]
		public void CanConstructUsingParameterlessConstructor()
		{
			// Arrange
			// Nothing to arrange.

			// Act
			IRootPathProvider rootPathProvider = new AspNetRootPathProvider();

			// Assert
			Assert.That(rootPathProvider, Is.Not.Null);
		}

		[Test]
		public void CanConstructUsingConstructorWithLocatorArgument()
		{
			// Arrange
			HttpContextLocator locator = new HttpContextLocator();

			// Act
			IRootPathProvider rootPathProvider = 
				new AspNetRootPathProvider(locator);

			// Assert
			Assert.That(rootPathProvider, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "httpContextLocator", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructUsingConstructorWithNullLocatorArgument()
		{
			// Arrange
			HttpContextLocator locator = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			// ReSharper disable ObjectCreationAsStatement
			new AspNetRootPathProvider(locator);
			// ReSharper restore ObjectCreationAsStatement
			// ReSharper restore ExpressionIsAlwaysNull

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanGetRootPathsUsingConstructorWithLocator()
		{
			// Arrange
			const string basePath = @"C:\Mock\BasePath";
			const string expectedPath1 = @"C:\Mock\BasePath\";
			const string expectedPath2 = @"C:\Mock\BasePath\bin";
			MockHttpContext httpContext = new MockHttpContext{ ServerUtility = new MockHttpServerUtility{ BasePath = basePath } };
			HttpContextLocator locator = new HttpContextLocator(httpContext);
			IRootPathProvider rootPathProvider = new AspNetRootPathProvider(locator);

			// Act
			IList<string> paths = rootPathProvider.GetRootPaths();

			// Assert
			Assert.That(paths.Count, Is.EqualTo(2));
			Assert.That(paths[0], Is.EqualTo(expectedPath1));
			Assert.That(paths[1], Is.EqualTo(expectedPath2));
		}

		[Test]
		public void CannotGetRootPathsUsingParameterlessConstructorAndNotInAspNetWebApp()
		{
			// Arrange
			IRootPathProvider rootPathProvider = new AspNetRootPathProvider();

			// Act
			IList<string> paths = rootPathProvider.GetRootPaths();

			// Assert
			Assert.That(paths, Is.Empty);
		}
	}
}
